package ar.com.ctrln.view.dashboard.mantenimiento.ticket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.mantenimiento.ticket.RegistrarTicket;
import ar.com.ctrln.persistence.model.cliente.Cliente;
import ar.com.ctrln.view.render.mantenimiento.ticket.SelectClienteRowRender;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class SeleccionarOrganizacionController extends AbstractMinimizarVentana {

	private static final long serialVersionUID = -6047378427213826430L;

	@WireVariable
	private RegistrarTicket registrarTicket;

	private Cliente emisor;

	@Wire
	private Window wSeleccionarCliente;

	@Wire
	private Listbox lbClientes;

	@Wire
	private Button btnSeleccionarCliente;

	@Wire("#txtFilter")
	private Textbox txtFilter;

	@Wire
	private Label footer_label;

	private ListModelList<Cliente> modelo;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		modelo = new ListModelList<Cliente>();
		modelo.addAll(registrarTicket.listaClientes());
		lbClientes.setModel(modelo);
		lbClientes.setItemRenderer(new SelectClienteRowRender());
	}

	@Listen("onClick = #btnSeleccionarCliente")
	public void traerSeleccionCliente() {
		if (lbClientes.getSelectedItem() != null) {
			emisor = (Cliente) lbClientes.getSelectedItem()
					.getAttribute("data");
			System.out.println(emisor.getEmail());
			this.wSeleccionarCliente.onClose();
			EventQueue<Event> eq = EventQueues.lookup("interactive",
					EventQueues.DESKTOP, false);
			eq.publish(new Event("onButtonClick", btnSeleccionarCliente, emisor));
		}
	}

	@Listen("onCreate = #lbClientes")
	public void listarClientes() {

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Listen("onChanging = #txtFilter")
	public void getFilter(Event event) {
		((Textbox) event.getTarget())
				.setValue(((org.zkoss.zk.ui.event.InputEvent) event).getValue());
		lbClientes.setModel(new ListModelList(getFilterBy(txtFilter.getValue(),
				modelo.getInnerList())));
		if (lbClientes.getItemCount() > 0) {
			footer_label.setValue("");
			footer_label.setValue("Un total de " + lbClientes.getItemCount()
					+ " Clientes");
		} else {
			footer_label.setValue("Ningun Resultado...");
			footer_label.setValue("");
		}
		((Textbox) event.getTarget()).setFocus(true);
	}

	public static List<Cliente> getFilterBy(String condicion,
			Collection<Cliente> clientes) {
		List<Cliente> data = new ArrayList<Cliente>();
		for (Iterator<Cliente> i = clientes.iterator(); i.hasNext();) {
			Cliente tmp = i.next();
			if (tmp.getRazonSocial().toLowerCase()
					.indexOf(condicion.trim().toLowerCase()) >= 0
					|| tmp.getTipoActividad().getNombreTipoActividad()
							.toLowerCase()
							.indexOf(condicion.trim().toLowerCase()) >= 0) {
				data.add(tmp);
			}
		}
		return data;
	}

}
